if (get_cookie("key")) {
    $.ajax({
        url: ip + "index.php?s=admincms/login/isadmin",
        type: "post",
        data: {
            "user": get_cookie("user"),
            "key": get_cookie("key")
        },
        success: function (_res) {
           var res = JSON.parse(_res);
            if (res.resStatus === 0) {
                // 用户写入页面
                $("#user_name").text(res.data[0].NickName);
                $(".content_left>ul").empty();
                var url=location.href;
                var tiao=true;
                $.each(res.data[0].power_list,function (i,o) {
                    if(url.indexOf(o.url)!==-1){
                        tiao=false;
                    }
                    var _background_=  url.indexOf(o.url)!==-1?'style="background:#000;"':"";

                    $(".content_left>ul").append('<a href="'+o.url+'"> <li '+_background_+'><span class="glyphicon '+o.icon+'"></span><p>'+o.names+'</p></li></a>');
                });

                if(tiao){
                    location.href=res.data[0].power_list[0].url;
                }
            }
            if (res.resStatus === 1) {
                location.href = key_address;
            }
        },
        error: function () {
            location.href = key_address;
        }
    });
} else {
    location.href = key_address;
}
$(function () {
    var houtai = {
        //防止频繁操作
        old_click_time: 1000,
        click_yes: function () {
            var now_tiem = new Date().getTime();
            if (now_tiem - houtai.old_click_time > 1000) {
                houtai.old_click_time = now_tiem;
                return true;
            } else {
                return false;
            }
        },
        linshi_search: {
            state: null,
            search: ""
        },
        linshi_id: null,
        linshi_zhekou_id:null,
        linshi_img_id: null,
        linshi_arrid: [],
        queren_type: null,//0删除 1上架 2下架 3全部上架 4全部下架 5删除图片 6上热卖 7下热卖
        linshi_rule:7,    //规则类型
        linshi_class_id:"",  //传品牌时需要的2级分类或3级分类ID
        list_ajax_page:1,
        //提示功能
        hint:function (str) {
            $(".hint").remove();
            $("body").append("<div class='hint'>"+str+"</div>");
            $(".hint").slideDown(300).delay(2000).slideUp();
        },
        jiancha: function () {
            // 判断是够为空
            for (var i = 0; i < $(".add_goods_body .input_is:visible").length; i++) {
                if ($(".add_goods_body .input_is:visible").eq(i).val() === "") {
                    $(".add_goods_body .input_is:visible").eq(i).focus();
                    houtai.hint("请填写 " + $(".add_goods_body .input_is:visible").eq(i).prev().text());
                    return false;
                }
            }
            return true;
        },
        //初始选择类
        chushi: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/product/product",
                type: "post",
                data:{
                    token:get_cookie("key")
                },
                success: function (res) {
                    //写入操作分类table
                    if (JSON.parse(res).resStatus === 0) {
                        houtai.arr = JSON.parse(res).data;
                   var string11 = "<option value=''>请选择一级</option>",
                       string22 = "<option value=''>请选择二级</option>",
                       string33 = "<option value=''>请选择三级</option>",
                       string44 = "<option value=''>请选择品牌</option>",
                       string55 = "<option value=''>请选择商品</option>";
                        for (var ii = 0; ii < houtai.arr.length; ii++) {
                            string11 += '<option value="' + houtai.arr[ii].pid + '">' + houtai.arr[ii].typesnames + '</option>';
                        }
                        $("#add_zhekou_class1").empty().append(string11);
                        $("#add_zhekou_class2").empty().append(string22);
                        $("#add_zhekou_class3").empty().append(string33);
                        $("#add_zhekou_brand").empty().append(string44);
                        $("#add_zhekou_item").empty().append(string55);

                        $("#set_zhekou_class1").empty().append(string11);
                        $("#set_zhekou_class2").empty().append(string22);
                        $("#set_zhekou_class3").empty().append(string33);
                        $("#set_zhekou_brand").empty().append(string44);
                        $("#set_zhekou_item").empty().append(string55);
                    }
                },
                error:function () {
                    houtai.hint("初始分类请求失败！！");
                }

            });
        },
        // 初始规则
        rule_list:function () {
            $.ajax({
                url: ip + "index.php/admin/discount/act/showRule",
                type: "post",
                data: {
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                    if(res.resStatus===0){
                        var select_string='';
                        $.each(res.data,function (i,o) {
                            select_string+='<option value="'+o.ruleId+'">'+o.title+'</option>';
                        });
                        $("#add_rule").empty().append(select_string);
                        $("#set_rule").empty().append(select_string);
                    }

                }
            });
        },
        shop_list_ajax: function () {
            $.ajax({
                url: ip + "index.php/admin/discount/act/show",
                type: "post",
                data: {
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                        houtai.table_string(res);
                        console.log(res);
                }
            });
        },
        table_string: function (res) {
            // 写入table
            if (res.resStatus === 0) {
                var data = res.data;
                $("#huodong_table").empty().append(tpl.compile('' +
                    '<thead><tr><th>编号</th><th>活动标题</th><th>活动时间</th><th>活动折扣</th><th>活动规则</th><th>操作</th></tr></thead>' +
                    '<tbody>' +
                        '{{for(var i=0;i<$data.length;i++){}}' +
                        '<tr>' +
                            '<td class="activity_id">{{=$data[i].activityId}}</td>' +
                            '<td class="activity_title">{{=$data[i].title}}</td>' +
                            '<td class="activity_time">{{=$data[i].startTime}} - {{=$data[i].endTime}}</td>' +
                            '<td class="activity_scope"><b class="btn btn-default btn-xs">点击进入</b></td>' +
                            '<td class="activity_rule" data-id={{=$data[i].rule.ruleId}}>{{=$data[i].rule.title}}</td>' +
                            '<td class="activity_control" style="width: 100px">' +
                                '<button class="btn btn-danger btn-xs set">修改</button> ' +
                                '{{if($data[i].status===0){}}'+
                                  '<button class="btn btn-success btn-xs up_line">上线</button>' +
                                '{{}else{}}'+
                                  '<button class="btn btn-info btn-xs down_line">下线</button>' +
                                 '{{}}}'+
                            '</td>' +
                        '</tr>' +
                        '{{} }}' +
                    '</tbody>')(data));
            }else{
                $("#huodong_table").empty().append(tpl.compile('' +
                    '<thead><tr><th>编号</th><th>商家名称</th><th>商家电话</th><th>商家地址</th><th>商家坐标</th><th>经营范围</th><th>图片</th><th>操作</th></tr></thead>' +
                    '<tbody>' +
                    '<tr>' +
                    '<td colspan="8" class="text-center">无数据</td>'+
                    '</tr>'+
                    '</tbody>')());
            }
        },
        tianjia: function () {
            $.ajax({
                url:ip + "index.php/admin/discount/act/add",
                data:{
                    title:$.trim($("#add_title").val()),
                    ruleId:$.trim($("#add_rule").val()),
                    startTime:new Date($("#add_time").val().split(" - ")[0]).getTime()/1000,
                    endTime:new Date($("#add_time").val().split(" - ")[1]).getTime()/1000,
                    token: get_cookie("key")

                },
                type: "post",
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        $("#add_huodong input").val("");
                        houtai.shop_list_ajax();
                        houtai.hint("<p class='text-success'>添加成功了！</p>");
                        $("#add_huodong").fadeOut();
                    }else{
                        houtai.hint("<p>"+res.message+"！</p>");
                    }
                },
                error: function (res) {
                    houtai.hint("<p>添加失败了！</p>");
                }
            });
        },
        xiugai: function () {
            $.ajax({
                url:ip + "index.php/admin/discount/act/edit",
                data:{
                    actId:houtai.linshi_id,
                    title:$.trim($("#set_title").val()),
                    ruleId:$.trim($("#set_rule").val()),
                    startTime:new Date($("#set_time").val().split(" - ")[0]).getTime()/1000,
                    endTime:new Date($("#set_time").val().split(" - ")[1]).getTime()/1000,
                    token: get_cookie("key")
                },
                type: "post",
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        $("#set_huodong input").val("");
                        houtai.shop_list_ajax();
                        houtai.hint("<p class='text-success'>修改成功了！</p>");
                        $("#set_huodong").fadeOut();
                    }else{
                        houtai.hint("<p>"+res.message+"！</p>");
                    }
                },
                error: function (res) {
                    houtai.hint("<p>修改失败了！</p>");
                }
            });
        },
        down_line: function () {
            $.ajax({
                url: ip + "index.php/admin/discount/act/actStatus",
                type: "post",
                data: {
                    status: 0,
                    token: get_cookie("key"),
                    actId:houtai.linshi_id
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        houtai.hint("<p class='text-success'>下线成功了！</p>");
                        houtai.shop_list_ajax(houtai.list_ajax_page);
                        $("#modal_wrap").fadeOut();
                    } else {
                        houtai.hint(res.message);
                    }
                },
                error: function () {
                    houtai.hint("下架失败了！！");
                }
            });
        },
        up_line: function () {
            $.ajax({
                url:ip + "index.php/admin/discount/act/actStatus",
                type: "post",
                data: {
                    status: 1,
                    token: get_cookie("key"),
                    actId:houtai.linshi_id
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        houtai.hint("<p class='text-success'>下线成功了！</p>");
                        houtai.shop_list_ajax(houtai.list_ajax_page);
                        $("#modal_wrap").fadeOut();
                    } else {
                        houtai.hint(res.message);
                    }
                },
                error: function () {
                    houtai.hint("上线失败了");
                }
            });
        },
        zhekou_list_ajax: function () {
            $.ajax({
                url: ip + "index.php/admin/discount/show",
                type: "post",
                data: {
                    activityId:houtai.linshi_id,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                        houtai.zhekou_table_string(res);
                }
            });
        },
        zhekou_table_string: function (res) {
            // 写入table
            if (res.resStatus === 0) {
                var data = res.data;
                $("#zhekou_table").empty().append(tpl.compile('' +
                    '<thead><tr><th>编号</th><th>折扣力度</th><th>发放数量</th><th>限购数量</th><th>最低数量</th><th>是否与券共用</th><th>折扣范围</th><th>折扣描述</th><th>操作</th></tr></thead>' +
                    '<tbody>' +
                        '{{for(var i=0;i<$data.length;i++){}}' +
                        '<tr>' +
                            '<td class="zhekou_id">{{=$data[i].dataId}}</td>' +
                            '<td class="zhekou_range">{{=$data[i].discount}}</td>' +
                            '<td class="zhekou_number">{{=$data[i].stock}}</td>' +
                            '<td class="zhekou_limit">{{=$data[i].restrictBuy}}</td>' +
                            '<td class="zhekou_min">{{=$data[i].minBuy}}</td>' +
                            '<td class="zhekou_coupon">{{=$data[i].isCoupon?"是":"否"}}</td>' +
                            '<td class="zhekou_scope"  data-id="{{=$data[i].catId===null?"":$data[i].catId}}"><b class="btn btn-default btn-xs">点击查看</b></td>' +
                            '<td class="zhekou_describe">{{=$data[i].describe}}</td>' +
                            '<td class="zhekou_control" style="width: 100px">' +
                                '<button class="btn btn-warning btn-xs zhekou_set">修改</button> ' +
                                '<button class="btn btn-danger btn-xs zhekou_remove">删除</button>' +
                            '</td>' +
                        '</tr>' +
                    '<tr class="hidden_">' +
                    '<td colspan="8">' +
                    '<ul>'+
                    '{{if($data[i].scope){}}'+
                        '{{var $arr=$data[i].scope; for(var a=0;a<$arr.length;a++){}}' +
                        '<li class="class2_item" data-id="{{=$arr[a].catId}}">{{=$arr[a].catTitle}}</li>'+
                        '{{} }}' +
                    '{{ } }}'+
                    '</ul>'+
                    '</td>' +
                    '</tr>' +
                        '{{} }}' +
                    '</tbody>')(data));
            }else{
                $("#zhekou_table").empty().append(tpl.compile('' +
                    '<thead><tr><th>编号</th><th>商家名称</th><th>商家电话</th><th>商家地址</th><th>商家坐标</th><th>经营范围</th><th>图片</th><th>操作</th></tr></thead>' +
                    '<tbody>' +
                    '<tr>' +
                    '<td colspan="8" class="text-center">无数据</td>'+
                    '</tr>'+
                    '</tbody>')());
            }
        },
        zhekou_add: function () {
            for(var i=0,arr=[];i<$("#add_zhekou .selected_item").length;i++){
                arr.push($("#add_zhekou .selected_item").eq(i).attr("data-id"));
            }

                $.ajax({
                    url:ip + "index.php/admin/discount/add",
                    data:{
                        actId:houtai.linshi_id,
                        id:arr,
                        catId:houtai.linshi_class_id,
                        stock:$.trim($("#add_zhekou_number").val()),
                        restrictBy:$.trim($("#add_zhekou_limit").val()),
                        discount:$.trim($("#add_zhekou_range").val()),
                        minBuy:$.trim($("#add_zhekou_min").val()),
                        isCoupon:$("#add1_zhekou_coupon").val(),
                        describe:$.trim($("#add_zhekou_describe").val()),
                        token: get_cookie("key")
                    },
                    type: "post",
                    success: function (_res) {
                        var res = JSON.parse(_res);
                        if (res.resStatus === 0) {
                            $("#add_zhekou input").val("");

                            var string11 = "<option value=''>请选择一级</option>",
                                string22 = "<option value=''>请选择二级</option>",
                                string33 = "<option value=''>请选择三级</option>",
                                string44 = "<option value=''>请选择品牌</option>",
                                string55 = "<option value=''>请选择商品</option>";
                            for (var ii = 0; ii < houtai.arr.length; ii++) {
                                string11 += '<option value="' + houtai.arr[ii].pid + '">' + houtai.arr[ii].typesnames + '</option>';
                            }
                            $("#add_zhekou_class1").empty().append(string11);
                            $("#add_zhekou_class2").empty().append(string22);
                            $("#add_zhekou_class3").empty().append(string33);
                            $("#add_zhekou_brand").empty().append(string44);
                            $("#add_zhekou_item").empty().append(string55);
                            $("#add_zhekou .selected_wrap").empty();
                            houtai.zhekou_list_ajax();
                            houtai.hint("<p style='color: #0f0'>添加成功了！</p>");
                            $("#add_zhekou").fadeOut();
                        }else{
                            houtai.hint("<p>"+res.message+"！</p>");
                        }
                    },
                    error: function (res) {
                        houtai.hint("<p>添加失败了！</p>");
                    }
                });
        },
        zhekou_set: function () {
            for(var i=0,arr=[];i<$("#set_zhekou .selected_item").length;i++){
                arr.push($("#set_zhekou .selected_item").eq(i).attr("data-id"));
            }

                $.ajax({
                    url:ip + "index.php/admin/discount/edit",
                    data:{
                        dataId:houtai.linshi_zhekou_id,
                        actId:houtai.linshi_id,
                        id:arr,
                        catId:houtai.linshi_class_id,
                        stock:$.trim($("#set_zhekou_number").val()),
                        restrictBy:$.trim($("#set_zhekou_limit").val()),
                        discount:$.trim($("#set_zhekou_range").val()),
                        minBuy:$.trim($("#set_zhekou_min").val()),
                        isCoupon:$("#set1_zhekou_coupon").val(),
                        describe:$.trim($("#set_zhekou_describe").val()),
                        token: get_cookie("key")

                    },
                    type: "post",
                    success: function (_res) {
                        var res = JSON.parse(_res);
                        if (res.resStatus === 0) {
                            $("#set_zhekou input").val("");
                            houtai.zhekou_list_ajax();
                            houtai.hint("<p style='color: #0f0'>修改成功了！</p>");
                            $("#set_zhekou").fadeOut();
                        }else{
                            houtai.hint("<p>"+res.message+"！</p>");
                        }
                    },
                    error: function (res) {
                        houtai.hint("<p>添加失败了！</p>");
                    }
                });
        },
        zhekou_remove: function () {
                $.ajax({
                    url:ip + "index.php/admin/discount/del",
                    data:{
                        dataId:houtai.linshi_zhekou_id,
                        token: get_cookie("key")
                    },
                    type: "post",
                    success: function (_res) {
                        var res = JSON.parse(_res);
                        if (res.resStatus === 0) {
                            houtai.zhekou_list_ajax();
                            houtai.hint("<p style='color: #0f0'>删除成功了！</p>");
                            $("#modal_wrap").fadeOut();
                        }else{
                            houtai.hint("<p>"+res.message+"！</p>");
                        }
                    },
                    error: function (res) {
                        houtai.hint("<p>请求失败了！</p>");
                    }
                });
        },
        // 判断是否存在
        is_exist:function(n,$element){
            var is_exist=false;
            $.each($element.find(".selected_item"),function(i,o){
              if(n==$(o).attr("data-id")){
                  is_exist=true;
                  return false;
              }
            });
            return is_exist;
        }
    };
    // 请求数据
    houtai.rule_list();
    houtai.chushi();
    houtai.shop_list_ajax();
    //时间插件
    $('#add_time').daterangepicker({
        timePicker: true,
        timePickerIncrement: 30,
        locale: {
            format: 'YYYY-MM-DD HH:mm',
            applyLabel: '应用',
            cancelLabel: '取消',
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        }
    });    //时间插件
    $('#set_time').daterangepicker({
        timePicker: true,
        timePickerIncrement: 30,
        locale: {
            format: 'YYYY-MM-DD HH:mm',
            applyLabel: '应用',
            cancelLabel: '取消',
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        }
    });
    // 添加活动按钮
    $("#add_btn").bind("click",function () {
        $("#add_huodong").fadeIn();
    });
    // 修改商家按钮
    $("#set").bind("click", function () {
        if(houtai.jiancha()){
                houtai.xiugai();
        }
    });
    // 添加按钮
    $("#add").bind("click", function () {
        if(houtai.jiancha()){
                houtai.tianjia();
        }
    });
    // 查看折扣
    $("#huodong_table").on("click",".activity_scope>b",function () {
        $(".selected_wrap").empty();
       houtai.linshi_rule=Number($(this).parents("tr").find(".activity_rule").attr("data-id"));
       houtai.linshi_id = $(this).parents("tr").find(".activity_id").text();
       console.log(houtai.linshi_rule);
        var string11 = "<option value=''>请选择一级</option>",
            string22 = "<option value=''>请选择二级</option>",
            string33 = "<option value=''>请选择三级</option>",
            string44 = "<option value=''>请选择品牌</option>",
            string55 = "<option value=''>请选择商品</option>";
        for (var ii = 0; ii < houtai.arr.length; ii++) {
            string11 += '<option value="' + houtai.arr[ii].pid + '">' + houtai.arr[ii].typesnames + '</option>';
        }
        $("#add_zhekou_class1").empty().append(string11);
        $("#add_zhekou_class2").empty().append(string22);
        $("#add_zhekou_class3").empty().append(string33);
        $("#add_zhekou_brand").empty().append(string44);
        $("#add_zhekou_item").empty().append(string55);
       $("#zhekou").fadeIn();


       houtai.zhekou_list_ajax();
    });
    // 修改
    $("#huodong_table").on("click", ".set", function () {
        $(window).scrollTop(0);
        houtai.linshi_id = $(this).parents("tr").find(".activity_id").text();
        $("#set_rule").val($(this).parents("tr").find(".activity_rule").attr("data-id"));
        $("#set_title").val($(this).parents("tr").find(".activity_title").text());
        $("#set_time").val($(this).parents("tr").find(".activity_time").text());
        $("#set_huodong").fadeIn(200);
    });
    // 上线按钮
    $("#huodong_table").on("click", ".up_line", function () {
        houtai.linshi_id = $(this).parents("tr").find(".activity_id").text();
        houtai.queren_type = 1;
        $("#modal_wrap").find(".modal-body").empty().append("你确定要上架<span style='color:#f10;'>" + $(this).parents("tr").find('.shangjia_name').text() + "</span>吗？");
        $("#modal_wrap").show();
    });
    // 下线按钮
    $("#huodong_table").on("click", ".down_line", function () {
        houtai.linshi_id = $(this).parents("tr").find(".activity_id").text();
        houtai.queren_type = 2;
        $("#modal_wrap").find(".modal-body").empty().append("你确定要下架<span style='color:#f10;'>" + $(this).parents("tr").find('.shangjia_name').text() + "</span>吗？");
        $("#modal_wrap").show();
    });

    // 确认
    $("#confirm").bind("click", function () {
        // 上线
        if (houtai.queren_type === 1) {
            houtai.up_line();
        }
        // 下线
        if (houtai.queren_type === 2) {
            houtai.down_line();
        } // 删除折扣
        if (houtai.queren_type === 3) {
            houtai.zhekou_remove();
        }

    });


// 折扣管理部分
    $("#add_zhekou_btn").bind("click",function () {
        $("#add_zhekou").fadeIn();
        $("select[id^=add_zhekou]").parents("[class*=row]").show();
        $("select[id^=add_zhekou]").parents("[class*=col]").show();
        $("#add_zhekou .selected_wrap").parents(".row").show();
        if(houtai.linshi_rule===1){
            $("select[id^=add_zhekou]").parents("[class*=row]").hide();
            $("#add_zhekou .selected_wrap").parents(".row").hide();
        }else if(houtai.linshi_rule===2){
            $("select[id^=add_zhekou]:not([id$=class1])").parents("[class*=col]").hide();
            $("#add_zhekou_brand").parents(".row").hide();
        }else if(houtai.linshi_rule===3){
            $("#add_zhekou_class3").parents("[class*=col]").hide();
            $("#add_zhekou_brand").parents(".row").hide();
        }else if(houtai.linshi_rule===4){
            $("#add_zhekou_item").parents(".row").hide();
        }else if(houtai.linshi_rule===5){
            $("#add_zhekou_class3").parents("[class*=col]").hide();
            $("#add_zhekou_item").parents("[class*=col]").hide();
        }else if(houtai.linshi_rule===6){
            $("#add_zhekou_item").parents("[class*=col]").hide();
        }else if(houtai.linshi_rule===7){
            $("#add_zhekou_brand").parents("[class*=col]").hide();
        }
    });
    $("#zhekou_table").on("click",".zhekou_scope>b",function () {
       $(this).parents("tr").next().fadeToggle();
    });
    $("#zhekou_table").on("click",".zhekou_set",function () {
      houtai.linshi_zhekou_id=$(this).parents("tr").find(".zhekou_id").text();
      houtai.linshi_class_id=$(this).parents("tr").find(".zhekou_scope").attr("data-id");
      console.log(houtai.linshi_class_id);
      $("#set_zhekou_range").val($(this).parents("tr").find(".zhekou_range").text());
      $("#set_zhekou_number").val($(this).parents("tr").find(".zhekou_number").text());
      $("#set_zhekou_limit").val($(this).parents("tr").find(".zhekou_limit").text());
      $("#set_zhekou_min").val($(this).parents("tr").find(".zhekou_min").text());
      $("#set_zhekou_describe").val($(this).parents("tr").find(".zhekou_describe").text());
      $("#set1_zhekou_coupon").val($(this).parents("tr").find(".zhekou_coupon").text()==="是"?1:0);
      var string='';
      var each_element=$(this).parents("tr").next().find(".class2_item");
      $.each(each_element,function(i,o){
          string+='<div class="selected_item" data-id="'+$(o).attr("data-id")+'"><p>'+$(o).text()+'</p><b class="glyphicon glyphicon-remove"></b></div>'
      });
        $("#set_zhekou .selected_wrap").empty().append(string);

        var string11 = "<option value=''>请选择一级</option>",
            string22 = "<option value=''>请选择二级</option>",
            string33 = "<option value=''>请选择三级</option>",
            string44 = "<option value=''>请选择品牌</option>",
            string55 = "<option value=''>请选择商品</option>";
        for (var ii = 0; ii < houtai.arr.length; ii++) {
            string11 += '<option value="' + houtai.arr[ii].pid + '">' + houtai.arr[ii].typesnames + '</option>';
        }
        $("#set_zhekou_class1").empty().append(string11);
        $("#set_zhekou_class2").empty().append(string22);
        $("#set_zhekou_class3").empty().append(string33);
        $("#set_zhekou_brand").empty().append(string44);
        $("#set_zhekou_item").empty().append(string55);

        $("select[id^=set_zhekou]").parents("[class*=row]").show();
        $("select[id^=set_zhekou]").parents("[class*=col]").show();
        $("#set_zhekou .selected_wrap").parents(".row").show();

        $("#set_zhekou").fadeIn();
        if(houtai.linshi_rule===1){
            $("select[id^=set_zhekou]").parents("[class*=row]").hide();
            $("#set_zhekou .selected_wrap").parents(".row").hide();
        }else if(houtai.linshi_rule===2){
            $("select[id^=set_zhekou]:not([id$=class1])").parents("[class*=col]").hide();
            $("#set_zhekou_brand").parents(".row").hide();
        }else if(houtai.linshi_rule===3){
            $("#set_zhekou_class3").parents("[class*=col]").hide();
            $("#set_zhekou_brand").parents(".row").hide();
        }else if(houtai.linshi_rule===4){
            $("#set_zhekou_item").parents(".row").hide();
        }else if(houtai.linshi_rule===5){
            $("#set_zhekou_class3").parents("[class*=col]").hide();
            $("#set_zhekou_item").parents("[class*=col]").hide();
        }else if(houtai.linshi_rule===6){
            $("#set_zhekou_item").parents("[class*=col]").hide();
        }else if(houtai.linshi_rule===7){
            $("#set_zhekou_brand").parents("[class*=col]").hide();
        }
    });
    $("#zhekou_table").on("click",".zhekou_remove",function () {
        houtai.queren_type =3;
        houtai.linshi_zhekou_id=$(this).parents("tr").find(".zhekou_id").text();
        $("#modal_wrap").fadeIn();
        $(".modal-body").empty().append("您确定要删除"+houtai.linshi_zhekou_id+"折扣吗？");
    });



// 折扣修改部分
    // 修改一级分类选择
    $("#set_zhekou_class1").bind("change",function () {
        var val=$(this).val();
        var select2_string='<option value="">请选择二级</option>';
        if(val!==""){
            if(houtai.linshi_rule===2){
                if(!houtai.is_exist($(this).val(),$("#set_zhekou"))){
                    $("#set_zhekou .selected_wrap").append('' +
                        '<div class="selected_item" data-id="'+$(this).val()+'">' +
                        '<p>'+$("#set_zhekou_class1 :selected").text()+'</p>' +
                        '<b class="glyphicon glyphicon-remove"></b>' +
                        '</div>')
                }
            }else{
                $.each(houtai.arr,function (i,o) {
                    if(val==o.pid){
                        $.each(o.activity_list,function (i,o) {
                            select2_string+='<option value="'+o.cid+'">'+o.classnames+'</option>';
                        });
                        return false;
                    }
                });
            }



        }
        $("#set_zhekou_class2").empty().append(select2_string);

    });
    // 修改二级分类选择
    $("#set_zhekou_class2").bind("change",function () {
        var val=$(this).val();
        var class1_val=$("#set_zhekou_class1").val();
        var select3_string='<option value="">请选择三级</option>';
        var select4_string='<option value="">请选择品牌</option>';
        var select5_string='<option value="">请选择商品</option>';
        if(houtai.linshi_rule===5){
            $("#set_zhekou .selected_wrap").empty();
            if(val!==""){
                houtai.linshi_class_id=val;
                $.ajax({
                    url: ip + "index.php/admin/discount/getBrand",
                    type: "post",
                    data: {
                        token: get_cookie("key"),
                        classId:val
                    },
                    success: function (_res) {
                        var res = JSON.parse(_res);
                        var string='<option value="">请选择品牌</option>';
                        if (res.resStatus === 0) {

                            $.each(res.data,function (i,o) {
                                console.log(o)
                                string+='<option value="'+o.brandId+'">'+o.name+'</option>'
                            });
                            $("#set_zhekou_brand").empty().append(string);
                        } else {
                            houtai.hint(res.message);
                        }
                    },
                    error: function () {
                        houtai.hint("品牌获取失败了！！");
                    }
                });
            }

        }else if(val!==""){
            if(houtai.linshi_rule===3){
                if(!houtai.is_exist($(this).val(),$("#set_zhekou"))){
                    $("#set_zhekou .selected_wrap").append('' +
                        '<div class="selected_item" data-id="'+$(this).val()+'">' +
                        '<p>'+$("#set_zhekou_class2 :selected").text()+'</p>' +
                        '<b class="glyphicon glyphicon-remove"></b>' +
                        '</div>')
                }
            }else{
                $.each(houtai.arr,function (i,o) {
                    if(class1_val==o.pid){
                        $.each(o.activity_list,function (i,o) {
                            if(val==o.cid){
                                $.each(o.cats_list,function (i,o) {
                                    select3_string+='<option value="'+o.catId+'">'+o.catName+'</option>';
                                });
                                return false;
                            }
                        });
                        return false;
                    }
                });
            }


        }

        $("#set_zhekou_class3").empty().append(select3_string);
        $("#set_zhekou_brand").empty().append(select4_string);
        $("#set_zhekou_item").empty().append(select5_string);

    });
    // 修改三级分类选择
    $("#set_zhekou_class3").bind("change",function () {
        var val=$(this).val();
        var select4_string='<option value="">请选择品牌</option>';
        var select5_string='<option value="">请选择商品</option>';
        if(houtai.linshi_rule===6){
            $("#set_zhekou .selected_wrap").empty();
            if(val!==""){
                houtai.linshi_class_id=val;
                $.ajax({
                    url: ip + "index.php/admin/discount/getBrand",
                    type: "post",
                    data: {
                        token: get_cookie("key"),
                        catId:val
                    },
                    success: function (_res) {
                        var res = JSON.parse(_res);
                        var string='<option value="">请选择品牌</option>';
                        if (res.resStatus === 0) {

                            $.each(res.data,function (i,o) {
                                console.log(o)
                                string+='<option value="'+o.brandId+'">'+o.name+'</option>'
                            });
                            $("#set_zhekou_brand").empty().append(string);
                        } else {
                            houtai.hint(res.message);
                        }
                    },
                    error: function () {
                        houtai.hint("品牌获取失败了！！");
                    }
                });
            }

        }else if(val!==""){
            if(houtai.linshi_rule===4){
                if(!houtai.is_exist($(this).val(),$("#set_zhekou"))){
                    $("#set_zhekou .selected_wrap").append('' +
                        '<div class="selected_item" data-id="'+$(this).val()+'">' +
                        '<p>'+$("#set_zhekou_class3 :selected").text()+'</p>' +
                        '<b class="glyphicon glyphicon-remove"></b>' +
                        '</div>')
                }
            }else if(houtai.linshi_rule===7){
                // 请求3级分类下商品写入
                    $.ajax({
                        url: ip + "index.php/admin/discount/goodsList",
                        type: "post",
                        data: {
                            token: get_cookie("key"),
                            catId:val
                        },
                        success: function (_res) {
                            var res = JSON.parse(_res);
                            var string='<option value="">请选择品牌</option>';
                            if (res.resStatus === 0) {

                                $.each(res.data,function (i,o) {
                                    console.log(o);
                                    string+='<option value="'+o.id+'">'+o.name+'</option>'
                                });
                                $("#set_zhekou_item").empty().append(string);
                            } else {
                                houtai.hint(res.message);
                            }
                        },
                        error: function () {
                            houtai.hint("品牌获取失败了！！");
                        }
                    });
            }
        }
        $("#set_zhekou_brand").empty().append(select4_string);
        $("#set_zhekou_item").empty().append(select5_string);

    });
    // 修改品牌选择
    $("#set_zhekou_brand").bind("change",function () {
        var val=$(this).val();
        if(val!==""){
            if(!houtai.is_exist($(this).val(),$("#set_zhekou"))){
                $("#set_zhekou .selected_wrap").append('' +
                    '<div class="selected_item" data-id="'+$(this).val()+'">' +
                    '<p>'+$("#set_zhekou_brand :selected").text()+'</p>' +
                    '<b class="glyphicon glyphicon-remove"></b>' +
                    '</div>')
            }
        }

    });
    // 修改商品选择
    $("#set_zhekou_item").bind("change",function () {
        var val=$(this).val();
        if(val!==""){
            if(!houtai.is_exist($(this).val(),$("#set_zhekou"))){
                $("#set_zhekou .selected_wrap").append('' +
                    '<div class="selected_item" data-id="'+$(this).val()+'">' +
                    '<p>'+$("#set_zhekou_item :selected").text()+'</p>' +
                    '<b class="glyphicon glyphicon-remove"></b>' +
                    '</div>')
            }
        }

    });
    // 修改商家按钮
    $("#zhekou_set").bind("click", function () {
        if(houtai.jiancha()){
            var val=Number($("#set_zhekou_range").val());
            var number_=Number($("#set_zhekou_number").val());
            var limit=Number($("#set_zhekou_limit").val());
            var min=Number($("#set_zhekou_min").val());
            if(!Math.isInteger(val)||val>100||val<1){
                houtai.hint("打折力度必须为整数1-100之间");
                return;
            }
            if(!Math.isInteger(number_)||number_<1){
                houtai.hint("发放数量必须为整数不能小于1");
                return;
            }
            if(!Math.isInteger(limit)||limit<1||limit>number_){
                houtai.hint("限购数量必须为整数不能小于1，不能大于发放数量");
                return;
            }
            if(!Math.isInteger(min)||min<1||min>number_){
                houtai.hint("最低数量必须为整数不能小于1，不能大于发放数量");
                return;
            }
            if(houtai.linshi_rule===1){
                houtai.linshi_class_id=null;
                houtai.zhekou_set();
                return;
            }
            if($("#set_zhekou .selected_item").length===0){
                houtai.hint("请选择折扣范围");
                return;
            }

                switch (houtai.linshi_rule){
                    case 2:
                    case 3:
                    case 4:
                    case 7:houtai.linshi_class_id=null;
                        break;
                }
            houtai.zhekou_set();
        }
    });
    $("body").on("click",".selected_item>b",function () {
        $(this).parents(".selected_item").remove();
    });



 // 折扣添加部分
    // 添加一级分类选择
    $("#add_zhekou_class1").bind("change",function () {
        var val=$(this).val();
        var select2_string='<option value="">请选择二级</option>';
        if(val!==""){
            if(houtai.linshi_rule===2){
                if(!houtai.is_exist($(this).val(),$("#add_zhekou"))){
                    $("#add_zhekou .selected_wrap").append('' +
                        '<div class="selected_item" data-id="'+$(this).val()+'">' +
                        '<p>'+$("#add_zhekou_class1 :selected").text()+'</p>' +
                        '<b class="glyphicon glyphicon-remove"></b>' +
                        '</div>')
                }
            }else{
                $.each(houtai.arr,function (i,o) {
                    if(val==o.pid){
                        $.each(o.activity_list,function (i,o) {
                            select2_string+='<option value="'+o.cid+'">'+o.classnames+'</option>';
                        });
                        return false;
                    }
                });
            }



        }
        $("#add_zhekou_class2").empty().append(select2_string);

    });
    // 添加二级分类选择
    $("#add_zhekou_class2").bind("change",function () {
        var val=$(this).val();
        var class1_val=$("#add_zhekou_class1").val();
        var select3_string='<option value="">请选择三级</option>';
        var select4_string='<option value="">请选择品牌</option>';
        var select5_string='<option value="">请选择商品</option>';
        if(houtai.linshi_rule===5){
            $("#add_zhekou .selected_wrap").empty();
            if(val!==""){
                houtai.linshi_class_id=val;
                $.ajax({
                    url: ip + "index.php/admin/discount/getBrand",
                    type: "post",
                    data: {
                        token: get_cookie("key"),
                        classId:val
                    },
                    success: function (_res) {
                        var res = JSON.parse(_res);
                        var string='<option value="">请选择品牌</option>';
                        if (res.resStatus === 0) {

                            $.each(res.data,function (i,o) {
                                console.log(o)
                                string+='<option value="'+o.brandId+'">'+o.name+'</option>'
                            });
                            $("#add_zhekou_brand").empty().append(string);
                        } else {
                            houtai.hint(res.message);
                        }
                    },
                    error: function () {
                        houtai.hint("品牌获取失败了！！");
                    }
                });
            }

        }else if(val!==""){
            if(houtai.linshi_rule===3){
                if(!houtai.is_exist($(this).val(),$("#add_zhekou"))){
                    $("#add_zhekou .selected_wrap").append('' +
                        '<div class="selected_item" data-id="'+$(this).val()+'">' +
                        '<p>'+$("#add_zhekou_class2 :selected").text()+'</p>' +
                        '<b class="glyphicon glyphicon-remove"></b>' +
                        '</div>')
                }
            }else{
                $.each(houtai.arr,function (i,o) {
                    if(class1_val==o.pid){
                        $.each(o.activity_list,function (i,o) {
                            if(val==o.cid){
                                $.each(o.cats_list,function (i,o) {
                                    select3_string+='<option value="'+o.catId+'">'+o.catName+'</option>';
                                });
                                return false;
                            }
                        });
                        return false;
                    }
                });
            }


        }

        $("#add_zhekou_class3").empty().append(select3_string);
        $("#add_zhekou_brand").empty().append(select4_string);
        $("#add_zhekou_item").empty().append(select5_string);

    });
    // 添加三级分类选择
    $("#add_zhekou_class3").bind("change",function () {
        var val=$(this).val();
        var class1_val=$("#add_zhekou_class1").val();
        var select4_string='<option value="">请选择品牌</option>';
        var select5_string='<option value="">请选择商品</option>';
        if(houtai.linshi_rule===6){
            $("#add_zhekou .selected_wrap").empty();
            if(val!==""){
                houtai.linshi_class_id=val;
                $.ajax({
                    url: ip + "index.php/admin/discount/getBrand",
                    type: "post",
                    data: {
                        token: get_cookie("key"),
                        catId:val
                    },
                    success: function (_res) {
                        var res = JSON.parse(_res);
                        var string='<option value="">请选择品牌</option>';
                        if (res.resStatus === 0) {

                            $.each(res.data,function (i,o) {
                                console.log(o)
                                string+='<option value="'+o.brandId+'">'+o.name+'</option>'
                            });
                            $("#add_zhekou_brand").empty().append(string);
                        } else {
                            houtai.hint(res.message);
                        }
                    },
                    error: function () {
                        houtai.hint("品牌获取失败了！！");
                    }
                });
            }

        }else if(val!==""){
            if(houtai.linshi_rule===4){
                if(!houtai.is_exist($(this).val(),$("#add_zhekou"))){
                    $("#add_zhekou .selected_wrap").append('' +
                        '<div class="selected_item" data-id="'+$(this).val()+'">' +
                        '<p>'+$("#add_zhekou_class3 :selected").text()+'</p>' +
                        '<b class="glyphicon glyphicon-remove"></b>' +
                        '</div>')
                }
            }else if(houtai.linshi_rule===7){
                // 请求3级分类下商品写入
                $.ajax({
                    url: ip + "index.php/admin/discount/goodsList",
                    type: "post",
                    data: {
                        token: get_cookie("key"),
                        catId:val
                    },
                    success: function (_res) {
                        var res = JSON.parse(_res);
                        var string='<option value="">请选择品牌</option>';
                        if (res.resStatus === 0) {

                            $.each(res.data,function (i,o) {
                                console.log(o);
                                string+='<option value="'+o.id+'">'+o.name+'</option>'
                            });
                            $("#add_zhekou_item").empty().append(string);
                        } else {
                            houtai.hint(res.message);
                        }
                    },
                    error: function () {
                        houtai.hint("品牌获取失败了！！");
                    }
                });
            }
        }
        $("#add_zhekou_brand").empty().append(select4_string);
        $("#add_zhekou_item").empty().append(select5_string);

    });
    // 添加品牌选择
    $("#add_zhekou_brand").bind("change",function () {
        var val=$(this).val();
        if(val!==""){
            if(!houtai.is_exist($(this).val(),$("#add_zhekou"))){
                $("#add_zhekou .selected_wrap").append('' +
                    '<div class="selected_item" data-id="'+$(this).val()+'">' +
                    '<p>'+$("#add_zhekou_brand :selected").text()+'</p>' +
                    '<b class="glyphicon glyphicon-remove"></b>' +
                    '</div>')
            }
        }

    });
    // 添加商品选择
    $("#add_zhekou_item").bind("change",function () {
        var val=$(this).val();
        if(val!==""){
            if(!houtai.is_exist($(this).val(),$("#add_zhekou"))){
                $("#add_zhekou .selected_wrap").append('' +
                    '<div class="selected_item" data-id="'+$(this).val()+'">' +
                    '<p>'+$("#add_zhekou_item :selected").text()+'</p>' +
                    '<b class="glyphicon glyphicon-remove"></b>' +
                    '</div>')
            }
        }

    });
    // 折扣添加按钮
    $("#zhekou_add").bind("click", function () {
        if(houtai.jiancha()){
            var val=Number($("#add_zhekou_range").val());
            var number_=Number($("#add_zhekou_number").val());
            var limit=Number($("#add_zhekou_limit").val());
            var min=Number($("#add_zhekou_min").val());
            if(!Math.isInteger(val)||val>100||val<1){
                houtai.hint("打折力度必须为整数1-100之间");
                return;
            }
            if(!Math.isInteger(number_)||number_<1){
                houtai.hint("发放数量必须为整数不能小于1");
                return;
            }
            if(!Math.isInteger(limit)||limit<1||limit > number_){
                houtai.hint("限购数量必须为整数不能小于1，不能大于发放数量");
                return;
            }
            if(!Math.isInteger(min)||min<1||min > number_){
                houtai.hint("最低数量必须为整数不能小于1，不能大于发放数量");
                return;
            }
            if(houtai.linshi_rule===1){
                houtai.linshi_class_id=null;
                houtai.zhekou_add();
                return;
            }
            if($("#add_zhekou .selected_item").length===0){
                houtai.hint("请选择折扣范围");
                return;
            }
                switch (houtai.linshi_rule){
                    case 2:
                    case 3:
                    case 4:
                    case 7:houtai.linshi_class_id=null;
                    break;
                }
            houtai.zhekou_add();
        }
    });


    // 收展事件
    $(".right-top>b").bind("click", function () {
        if ($(".content_left").is(":hidden")) {
            $(".content_left").removeClass("ishidden");
            $(".content_right").removeClass("iskuozhang");
        } else {
            $(".content_left").addClass("ishidden");
            $(".content_right").addClass("iskuozhang");
        }
    });
    // 莫态框取消modal
    $("#modal_close").bind("click", function () {
        $("#modal_wrap").fadeOut(200);

    });
    // 取消操作
    $(".cancle_control").bind("click", function () {
        $(this).parents(".set_shangpin").fadeOut(200);
        $(".add_img_wrap").empty();
        $(".set_img_wrap").empty();
        $("#add_input_file").val("");
        $("#set_input_file").val("");
    });

    //其他信息查看展开收缩按钮
    $("#shangpin_table").on("click",".click_class2>b",function () {
      $(this).parents("tr").next().fadeToggle();
    });

    //退出按钮
    $("#exit").bind("click", function () {
        set_cookie("key", "", 0);
        location.href = key_address;
    });
    //头像按钮
    $(".right-top>div").bind("click", function (e) {
        $("#exit").slideToggle();
        e.stopPropagation();
    });
    $(window).bind("click", function () {
        $("#exit").slideUp();
    });
});

